# Get build the User ID list

HTTP_PAYLOAD_DIR = "./http_payload/laptop/"
GLOBAL_MAP = {}
SEPARATE_USERS_DIR = "./separate_users/"

module SeparateUsers
  def self.get_all_files
    Common.get_all_files(HTTP_PAYLOAD_DIR)    
  end

  def self.handle_all_files(all_files)
    counter = 0
    all_files.each do |file_name|
      handle_each_file file_name
      puts "-- " + file_name
    end
  end

  def self.get_time
    Time.now
  end

  def self.handle_each_file(file_name)
    log = File.open "log_separate.txt", "a"
    puts ">> now handling : " + file_name

    log.puts ">>[#{get_time}] now handling : " + file_name
    
    file = File.open HTTP_PAYLOAD_DIR + file_name, "r"

    begin
      while line = file.readline

        fields = line.split("|")
        client_ip = fields[5]

        ip_fields = client_ip.split "."

        if ip_fields.size != 4
          next
        end

        file_key = ip_fields[2]

        # select the db to store the lines
        output = get_target_file file_key

        output.puts line
      end
    rescue Exception => e
      puts e.to_s
    end

    # to close the file
    file.close

    log.close
  end

  def self.get_target_file(key)
    unless GLOBAL_MAP.has_key? key
      output = File.open SEPARATE_USERS_DIR + key + ".bcp", "a"
      GLOBAL_MAP[key] = output
    end

    GLOBAL_MAP[key]
  end

  def self.close_all_file
    GLOBAL_MAP.each_value do |output|
      output.close
    end
  end

  def self.run
    puts ">> start to build user databases ..."

    begin
      all_files = SeparateUsers.get_all_files      
      SeparateUsers.handle_all_files all_files
      puts ">> end to build user dbs"
    rescue Exception => e
      puts ">> EEROR:"
      puts e.to_s
    end
  end

end